package pogvue.gui.popups;

import pogvue.gui.AlignViewport;
import pogvue.gui.Controller;
import pogvue.gui.ThresholdPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public abstract class ThresholdPopup extends Popup {
  final ThresholdPanel tp;

  public ThresholdPopup(JPanel parent, AlignViewport av, Controller c, String title, String label, int low, int high, int value) {
    super(parent,av,c,title);

    tp = new ThresholdPanel(parent,label,low,high, value);
  }

  void setValue(int value) {
    tp.setValue(value);
  }

  public abstract class ThresholdAction extends JalPopupAction {
    public ThresholdAction(String name, AlignViewport av,Controller c) {
      super(name,av,c);
      addRequiredArg("threshold");
    }

    public void getArgsFromSource(Button but, ActionEvent evt) {
      int threshold;
      super.getArgsFromSource(but,evt);
      try {
        threshold = Integer.valueOf(tp.getText());
      } catch (Exception ex) {
        threshold = tp.getSBValue();
        tp.setText(threshold);
      }
      putArg("threshold",tp.getText());
    }
 
    public void updateActionState(ActionEvent evt) {
      super.updateActionState(evt);
 
      if (containsArg("threshold")) {
        tp.setText(getArg("threshold"));
      }
    }

    public abstract void applyAction(ActionEvent evt);

    int getThreshold() {
      int threshold;
      try {
        threshold = Integer.valueOf(getArg("threshold"));
      } catch (Exception ex) {
        threshold = 0;
      }
      return threshold;
    }
  }
}
